package com.wehiking.webviewKotlin

import kotlinx.android.synthetic.main.activity_main.*
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.content.Context
import android.util.Log
import android.view.KeyEvent
import android.webkit.WebView
import android.widget.TextView
import android.view.View
import android.widget.ImageView
import android.support.v4.view.ViewPager.OnPageChangeListener
import android.view.LayoutInflater

//import com.example.administrator.webview_kotlin.R.id.webView


class MainActivity : AppCompatActivity() {
    /** 上下文 */
    private var context: Context? = null
    private var webView: WebView? = null

    private var views = ArrayList<View>()
    private var ivCurrent: ImageView? = null
    private var tvCurrent: TextView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //加载布局
        setContentView(R.layout.activity_main)
        context = this

        initView()
        initData()
        //initWebview()
    }


//    private fun initWebview() {
//        webView = findViewById<View>(R.id.webView) as WebView
//        val webView = webView
//        if (webView != null) {
//            webView.settings.javaScriptEnabled = true
//            webView.settings.javaScriptCanOpenWindowsAutomatically = true
//            webView.settings.setSupportMultipleWindows(true)
//            webView.settings.domStorageEnabled = true
//
//            webView.webChromeClient = WebChromeClient()
//            webView.webViewClient = object : WebViewClient() {
//                override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
//                    view.loadUrl(url)
//                    return false
//                }
//            }
//
////            val wvc = Aaaa()
////            web.setWebViewClient(wvc)
//
//            webView.loadUrl("http://baidu.com")
//        }
//
//    }

    //重写keydown事件
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //如果点击的是后退键  首先判断webView是否能够后退
            //如果点击的是后退键  首先判断webView是否能够后退   返回值是boolean类型的
            val webView = webView
            if (webView != null && webView.canGoBack()) {
                webView.goBack()
                return true
            }
        }
        return super.onKeyDown(keyCode, event)
    }

    private fun initView() {
        val onclick = object : View.OnClickListener {
            override fun onClick(v: View) {
                changeTab(v.id)
            }
        }

        llChat.setOnClickListener(onclick)
        llFriends.setOnClickListener(onclick)
        llContacts.setOnClickListener(onclick)
        llSettings.setOnClickListener(onclick)

        ivChat.isSelected = true
        tvChat.isSelected = true

        ivCurrent = ivChat
        tvCurrent = tvChat

        val vp = object : OnPageChangeListener {

            override fun onPageSelected(position: Int) {
                changeTab(position)
            }

            override fun onPageScrolled(arg0: Int, arg1: Float, arg2: Int) {

            }

            override fun onPageScrollStateChanged(arg0: Int) {

            }
        }

        viewPager.addOnPageChangeListener(vp)

//        button.setOnClickListener({
//            finish()
//        })
    }

    private fun initData() {
        val mInflater = LayoutInflater.from(this)
        val tab01 = mInflater.inflate(R.layout.tab01, null)
        val tab02 = mInflater.inflate(R.layout.tab02, null)
        val tab03 = mInflater.inflate(R.layout.tab03, null)
        val tab04 = mInflater.inflate(R.layout.tab04, null)
        views.add(tab01)
        views.add(tab02)
        views.add(tab03)
        views.add(tab04)

        val adapter = MyPagerAdapter(views)
        viewPager.adapter = adapter
    }

    private fun changeTab(id: Int) {
        ivCurrent?.isSelected = false
        tvCurrent?.isSelected = false
        when (id) {
        //---
            R.id.llChat -> {
                viewPager.currentItem = 0
                ivChat.isSelected = true
                ivCurrent = ivChat
                tvChat.isSelected = true
                tvCurrent = tvChat
            }
            0 -> {
                ivChat.isSelected = true
                ivCurrent = ivChat
                tvChat.isSelected = true
                tvCurrent = tvChat
            }
        //---
            R.id.llFriends -> {
                viewPager.currentItem = 1
                ivFriends.isSelected = true
                ivCurrent = ivFriends
                tvFriends.isSelected = true
                tvCurrent = tvFriends
            }
            1 -> {
                ivFriends.isSelected = true
                ivCurrent = ivFriends
                tvFriends.isSelected = true
                tvCurrent = tvFriends
            }
        //---
            R.id.llContacts -> {
                viewPager.currentItem = 2
                ivContacts.isSelected = true
                ivCurrent = ivContacts
                tvContacts.isSelected = true
                tvCurrent = tvContacts
            }
            2 -> {
                ivContacts.isSelected = true
                ivCurrent = ivContacts
                tvContacts.isSelected = true
                tvCurrent = tvContacts
            }
        //---
            R.id.llSettings -> {
                viewPager.currentItem = 3
                ivSettings.isSelected = true
                ivCurrent = ivSettings
                tvSettings.isSelected = true
                tvCurrent = tvSettings
            }
            3 -> {
                ivSettings.isSelected = true
                ivCurrent = ivSettings
                tvSettings.isSelected = true
                tvCurrent = tvSettings
            }
            else -> {
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("", "onDestroy()====================")
    }
}